package com.osce.basic.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.csce.domain.entity.basic.TabAssetInventory;
import com.csce.domain.entity.basic.vo.TabAssetInventoryExcel;
import com.osce.interfaces.basic.ITabAssetInventoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 固定资产管理Controller
 *
 * @author hdj
 * @since 2025-11-15
 */
@RestController
@RequestMapping("/basic/inventory")
public class TabAssetInventoryController extends BaseController {

    @Resource
    private ITabAssetInventoryService tabAssetInventoryService;

/**
 * 查询固定资产管理列表
 */
@PreAuthorize("@ss.hasPermi('basic:inventory:list')")
@GetMapping("/list")
    public TableDataInfo list(TabAssetInventory tabAssetInventory) {
        startPage();
        List<TabAssetInventory> list = tabAssetInventoryService.selectTabAssetInventoryList(tabAssetInventory);
        return getDataTable(list);
    }

    /**
     * 导出固定资产管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:inventory:export')")
    @Log(title = "固定资产管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TabAssetInventory tabAssetInventory) {
        List<TabAssetInventory> list = tabAssetInventoryService.selectTabAssetInventoryList(tabAssetInventory);
        ExcelUtil<TabAssetInventory> util = new ExcelUtil<TabAssetInventory>(TabAssetInventory. class);
        util.exportExcel(response, list, "固定资产管理数据");
    }

     /**
      * 导入固定资产管理列表
     */
    @Log(title = "固定资产管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basic:inventory:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<TabAssetInventory> util = new ExcelUtil<TabAssetInventory>(TabAssetInventory.class);
            List<TabAssetInventory> tabAssetInventoryList = util.importExcel(file.getInputStream());

            // 调用service层导入数据，此时即使部分数据失败，也会继续处理并返回完整结果
            String message = tabAssetInventoryService.importTabAssetInventory(tabAssetInventoryList);

            // 返回成功消息，包含导入结果详情
            return AjaxResult.success(message);
        } catch (Exception e) {
            // 捕获解析文件等非业务异常
            return AjaxResult.error("导入固定资产管理数据失败：" + e.getMessage());
        }
    }

        /**
     * 导出模板
     */
    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('basic:inventory:import')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TabAssetInventoryExcel> util = new ExcelUtil<>(TabAssetInventoryExcel.class);
        util.importTemplateExcel(response, "固定资产数据");
    }

    /**
     * 获取固定资产管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:inventory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tabAssetInventoryService.selectTabAssetInventoryById(id));
    }

    /**
     * 新增固定资产管理
     */
    @PreAuthorize("@ss.hasPermi('basic:inventory:add')")
    @Log(title = "固定资产管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TabAssetInventory tabAssetInventory) {
        return toAjax(tabAssetInventoryService.insertTabAssetInventory(tabAssetInventory));
    }

    /**
     * 修改固定资产管理
     */
    @PreAuthorize("@ss.hasPermi('basic:inventory:edit')")
    @Log(title = "固定资产管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TabAssetInventory tabAssetInventory) {
        return toAjax(tabAssetInventoryService.updateTabAssetInventory(tabAssetInventory));
    }

    /**
     * 删除固定资产管理
     */
    @PreAuthorize("@ss.hasPermi('basic:inventory:remove')")
    @Log(title = "固定资产管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tabAssetInventoryService.deleteTabAssetInventoryByIds(ids));
    }
}
